home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / DIUPDATE.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  4KB  |  136 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    diupdate.c
  5. //   Title:    Data File I/O Library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to update a logical file within a physical file.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <di.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //   Description:    Update the data area of a logical file.
  49. //    Parameters:    pcsz            Physical file name. 
  50. //                        pcszLogical    Logical file to update
  51. //                        usType        Logical file type
  52. //                        pcszFile        File name to append. The file must be able to 
  53. //                                        fit within the area allocated to the previous 
  54. //                                        file.
  55. //       Returns:    TRUE if successful.
  56. //----------------------------------------------------------------------------
  57. BOOL FN_E DioUpdate(PCSZ pcsz, PCSZ pcszLogical, USHORT usType, PCSZ pcszFile)
  58. {
  59.     HF hf1;
  60.     HF hf2 = -1;
  61.     HPF hpf;
  62.     HLF hlf = -1;
  63.     SIZET cDir;
  64.     DATADIR dir;
  65.     FLAG16 fs = FL_OPEN|FL_DENYREADWRITE|FL_BINARY;
  66.     FPOS fsize, flen;
  67.     BOOL fUser;
  68.     BOOL fResult = FALSE;
  69.  
  70.     if (HIUSHORT(pcsz))                        // Open physical file
  71.         {
  72.         if (!DioOpenPhysical(pcsz, &hpf, TRUE))
  73.             return FALSE;
  74.         fUser = FALSE;
  75.         }
  76.     else
  77.         {
  78.         hpf = LOUSHORT(pcsz);
  79.         Assert(hpf >= 0 && hpf < MAX_PHYSICAL_FILES);
  80.         Assert(di.physical[hpf].fUsed);
  81.         fUser = TRUE;
  82.         }
  83.                                                     // Not open logical file
  84.     if (!DioOpenLogical(pcszLogical, &hlf, usType))
  85.         goto ERROR_EXIT;
  86.  
  87.     cDir = di.logical[hlf].cDir;
  88.     hf1 = di.logical[hlf].hf;
  89.     flen = di.logical[hlf].flen;
  90.     if ((flen % (FPOS)sizeof(DATAHDR)) != 0)
  91.         {
  92.         flen += sizeof(DATAHDR);            // Round up to nearest multiple of
  93.         flen /= sizeof(DATAHDR);            //  header size
  94.         flen *= sizeof(DATAHDR);
  95.         }
  96.                                                     // Open external file
  97.     if (!FileOpen(&hf2, pcszFile, fs, NULL))
  98.         return FALSE;
  99.  
  100.     fsize = FileGetSize(hf2);                // Get size of external file
  101.     if (!FileSetPos(hf2, 0, FL_BEGIN))
  102.         goto ERROR_EXIT;
  103.     if (fsize > flen)                            // Check that new data will fit
  104.         {
  105.         Error("Updated data file is too large.");
  106.         goto ERROR_EXIT;
  107.         }                                            // Copy data
  108.     if (!FileCopy(hf1, di.logical[hlf].fbase, hf2, 0, fsize))
  109.         goto ERROR_EXIT;                        
  110.                                                     // Read directory
  111.     if (!DioDirRead(hpf, cDir, &dir))
  112.         goto ERROR_EXIT;
  113.  
  114.     dir.flen = fsize;
  115.                                                     // Calculate crc
  116.     if (!FileCrc(hf1, &dir.crcData, dir.flen, dir.fbase))
  117.         goto ERROR_EXIT;
  118.                                                     // Write updated header
  119.     if (!DioDirWrite(hpf, cDir, &dir))
  120.         goto ERROR_EXIT;
  121.  
  122.     fResult = TRUE;
  123.  
  124. ERROR_EXIT:
  125.     if (hf2 >= 0)
  126.         FileClose(hlf);
  127.     if (hlf >= 0)
  128.         DioCloseLogical(hlf);
  129.     if (!fUser)
  130.         DioClosePhysical(hpf);
  131.     return fResult;
  132. }
  133. //----------------------------------------------------------------------------
  134. //------------------------------- End of File --------------------------------
  135. //----------------------------------------------------------------------------
  136.